#include "sys.h"
#include "usart.h"	  
#include "delay.h"
u8  chuankou=10;
void USART2_init(u32 bound)                  
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	 NVIC_InitTypeDef NVIC_InitStructure;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
 	USART_DeInit(USART2);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;			
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 	
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;	//PA2
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽
	GPIO_Init(GPIOA, &GPIO_InitStructure);
   
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
	GPIO_Init(GPIOA, &GPIO_InitStructure);  
	
	 USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
	 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
	 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
	 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
	 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
	 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//收发模式
	
	 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	 NVIC_Init(&NVIC_InitStructure);
	
	 USART_Init(USART2, &USART_InitStructure); //初始化串口
   
	 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
	 USART_Cmd(USART2, ENABLE); 
	
	 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	 u2_RX=du;

}


void USART2_TxData(u8 data[], u16 length )
{
	u16 i;

	if (USART_GetFlagStatus( USART2, USART_SR_TXE ))
	{
		for(i = 0 ; i<length;i++ )
		{
			USART2->DR = (data[i] & (uint16_t)0x01FF);
			while (RESET == USART_GetFlagStatus( USART2, USART_SR_TXE ))
			{
				;/* code */
			}
		}
		USART2->DR = ( 0xFF );
		while (RESET == USART_GetFlagStatus( USART2, USART_SR_TXE ));
		USART2->DR = ( 0xFF );
		
	}
	
}
/*=====================================================================

		           串口中断

======================================================================*/
void USART2_IRQHandler(void)
{
	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
	{
		chuankou=USART_ReceiveData(USART2);
//		u2_RX=xie;
//		USART_SendData(USART2,chuankou);
//		while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
//		u2_RX=du;
	}
}
